package bm.com.framework.common.unit;

import jakarta.annotation.Resource;
import jakarta.servlet.ServletContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

@Slf4j
@Component
public class FileUtils {

    @Resource
    ServletContext servletContext;

    @Value("${sys.upload.path}")
    String path;

    /**
     * 保存文件信息
     * @param file
     * @param dirPath
     * @param fileName
     */
    public static void saveFile(MultipartFile file, String dirPath, String fileName) {
        File dir = new File(dirPath + File.separator + fileName);
        //判断文件父目录是否存在
        if (!dir.getParentFile().exists()) {
            dir.getParentFile().mkdirs();
        }
        try {
            //保存文件
            file.transferTo(dir);
        } catch (Exception e) {
//            log.info("Save file exception. {}", e.getMessage());
        }
    }

    /**
     * 获取文件前缀
     * @param fileName
     * @return
     */
    public static String getSuffix(String fileName) {
        return fileName.substring(fileName.lastIndexOf("."));
    }

    /**
     * 输出文件的新名称 (名字+后缀名)
     * @param fileName
     * @return
     */
    public static String generateRandomFileName(String fileName) {
        return UUID.randomUUID() + getSuffix(fileName);
    }

    /**
     * 获取文件路径
     * @return
     */
    public static String getFilepath() {
        return  "/Users/jgy/Documents/www/banmu-java/pmp/src/main/resources/static/uploads/" + DateUnit.timeToDate(DateUnit.getNowTimeMilliSecond(),"YYYY-MM-dd");
    }

    /**
     * 获取文件类型
     * @param name
     * @return
     */
    public static String fileContentType(String name) {
        String result = "";
        String fileType = name.toLowerCase();
        if (fileType.endsWith(".png")) {
            result = "image/png";
        } else if (fileType.endsWith(".gif")) {
            result = "image/gif";
        } else if (fileType.endsWith(".jpg") || fileType.endsWith(".jpeg")) {
            result = "image/jpeg";
        } else if (fileType.endsWith(".svg")) {
            result = "image/svg+xml";
        } else if (fileType.endsWith(".doc")) {
            result = "application/msword";
        } else if (fileType.endsWith(".xls")) {
            result = "application/x-excel";
        } else if (fileType.endsWith(".zip")) {
            result = "application/zip";
        } else if (fileType.endsWith(".pdf")) {
            result = "application/pdf";
        } else if (fileType.endsWith(".mpeg")) { //MP3
            result = "audio/mpeg";
        } else if (fileType.endsWith(".mp4")) {
            result = "video/mp4";
        } else if (fileType.endsWith(".plain")) {
            result = "text/plain";
        } else if (fileType.endsWith(".html")) {
            result = "text/html";
        } else if (fileType.endsWith(".json")) {
            result = "application/json";
        } else{
            result = "application/octet-stream";
        }
        return result;
    }

}
